<?php

/**
 * register actions.
 *
 * @package    PHOTO_GALLERY
 * @subpackage register
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class registerActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  public function executeStep1(sfWebRequest $request) {
      $this->form1 = new RegisterLogInfo();
      
      //populate with data if exist in session
      if ($this->getUser()->getAttribute('regForm1', FALSE)) {
          $this->form1->bind($this->getUser()->getAttribute('regForm1'));
          echo 'regForm in session';
      }
      if($request->isMethod('post')){
          echo 'success :P';
          $formInput = $request->getParameter('users_login');
          $this->form1->bind($formInput);
          if($this->form1->isValid()){
              $this->getUser()->setAttribute('regForm1', $formInput);
              $this->redirect('register/step2');
          }
          
      }
  }
  public function executeStep2(sfWebRequest $request) {
      //check proper navigation
      $regForm1 = $this->getUser()->getAttribute('regForm1', FALSE);
      if( ! $regForm1){
          $this->redirect('register/step1');
      }
      
      $this->form2 = new RegisterDetailsInfo();
      //populate with data if exist in session
      if ($this->getUser()->getAttribute('regForm2', FALSE)) {
          $formInput = (array)$this->getUser()->getAttribute('regForm2');
          $this->form2->bind($formInput);
      }
      if($request->isMethod('post')){
          $formInput = $request->getParameter('users_info');
          $this->form2->bind($formInput);
          var_dump($this->form2->isValid());
          if($this->form2->isValid()){
              echo 'didnt redirect';
              $this->getUser()->setAttribute('regForm2', $formInput);
              $this->redirect('register/step3');
          }
      }
  }
  
  public function executeStep3(sfWebRequest $request) {
      //check proper navigation
      $regForm1 = $this->getUser()->getAttribute('regForm1', FALSE);
      $regForm2 = $this->getUser()->getAttribute('regForm2', FALSE);
      if( ! $regForm1){
          $this->redirect('register/step1');
      }
      if(! $regForm2){
          $this->redirect('register/step2');
      }
     
$this->form3 = new RegisterTypeInfo();
//handle back button
      if ($this->getUser()->getAttribute('regForm3', FALSE)) {
          $this->form3->bind($this->getUser()->getAttribute('regForm3'));
      }
      elseif($request->isMethod('post')){
          $formInput = $request->getParameter('users_login');
          $this->form3->bind($formInput);
          if($this->form3->isValid()){
              //merge the 1st and 3rd form
//              $loginInfo = new UsersLoginForm();
//              $x = array_merge($formInput, $regForm1);
//              $loginInfo->bind($x);
//              var_dump($x);
//              echo '<br/>kkkkk';
//              if($loginInfo->isValid()){
//                  echo 'vallid';
//                  $loginInfo = $this->form3->getObject();
//              }
              
//              var_dump((array)$loginInfo);
//              $loginInfo->setPassword( md5($loginInfo->getPassword()) );//hash password
//              $loginInfo->save();
//              //save user detailed info
//              $infoForm = new UsersInfoForm();
//              $infoForm->bind($regForm2);
//              $details = $infoForm->geto
//              $details->setUserId($loginInfo->getUserId());
//              $details->save();
//              $this->redirect('user/index');
              
              $user = new UsersLogin();
              $user->setUsername($regForm1['username']);
              $user->setPassword( md5($regForm1['password']) );
              $user->setPackageId($formInput['package_id']);
              $user->save();
              
              $info = new UsersInfo();
              $info->setUserId($user->getUserId());
              $info->setName($regForm2['name']);
              $info->setEmail($regForm2['email']);
              $info->setAddress($regForm2['address']);
              $info->setCountry($regForm2['country']);
              $info->save();
              //clean up
              $this->getUser()->getAttributeHolder()->remove('regForm1'); 
              $this->getUser()->getAttributeHolder()->remove('regForm2');
              //login
              $this->getUser()->setAttribute('userId', $user->getUserId() );
              $this->redirect('user/index');
          }
          
      }
  }
  
}
